package com.mark.util;

import com.mark.common.RedisShardedPool;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.ShardedJedis;

/**
 * Created by Mark on 2018/4/20.
 * Redis api 封装工具类
 */

@Slf4j
public class RedisShardedPoolUtil {

    //设置key的有效期
    public static Long expire(String key,int seconds){
        ShardedJedis shardedJedis = null;
        Long result = null;
        try {
            shardedJedis = RedisShardedPool.getJedis();
            result = shardedJedis.expire(key,seconds);
        } catch (Exception e) {
            log.error("expire key:{} error",key ,e);
            RedisShardedPool.returnBrokenResource(shardedJedis);
            return result;
        }
        RedisShardedPool.returnResource(shardedJedis);
        return result;
    }

    public static String setEx(String key,String value,int seconds){
        ShardedJedis shardedJedis = null;
        String result = null;
        try {
            shardedJedis = RedisShardedPool.getJedis();
            result = shardedJedis.setex(key,seconds,value);
        } catch (Exception e) {
            log.error("expire key:{} value:{} error",key ,value,e);
            RedisShardedPool.returnBrokenResource(shardedJedis);
            return result;
        }
        RedisShardedPool.returnResource(shardedJedis);
        return result;
    }

    public static String set(String key,String value){
        ShardedJedis shardedJedis = null;
        String result = null;
        try {
            shardedJedis = RedisShardedPool.getJedis();
            result = shardedJedis.set(key,value);
        } catch (Exception e) {
            log.error("expire key:{} value:{} error",key ,value,e);
            RedisShardedPool.returnBrokenResource(shardedJedis);
            return result;
        }
        RedisShardedPool.returnResource(shardedJedis);
        return result;
    }

    public static String get(String key){
        ShardedJedis shardedJedis = null;
        String result = null;
        try {
            shardedJedis = RedisShardedPool.getJedis();
            result = shardedJedis.get(key);
        } catch (Exception e) {
            log.error("expire key:{} value:{} error",key ,e);
            RedisShardedPool.returnBrokenResource(shardedJedis);
            return result;
        }
        RedisShardedPool.returnResource(shardedJedis);
        return result;
    }

    public static Long del(String key){
        ShardedJedis shardedJedis = null;
        Long result = null;
        try {
            shardedJedis = RedisShardedPool.getJedis();
            result = shardedJedis.del(key);
        } catch (Exception e) {
            log.error("expire key:{} value:{} error",key ,e);
            RedisShardedPool.returnBrokenResource(shardedJedis);
            return result;
        }
        RedisShardedPool.returnResource(shardedJedis);
        return result;
    }


    public static void main(String[] args) {
        for(int i =0 ;i < 20 ;i++){
            RedisShardedPoolUtil.set(i+"",i+"");
       }
        /*String result = RedisShardedPoolUtil.get("0");
        System.out.println(result);*/
        System.out.println("end");
    }

}
